// server.go
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type SystemInfo struct {
	CPUUsage    float64 `json:"cpu_usage"`
	MemoryUsage float64 `json:"memory_usage"`
	// 可以根据需求添加其他字段
}

func handleSystemInfo(w http.ResponseWriter, r *http.Request) {
	var info SystemInfo

	// TODO: 处理从 Agent 发来的信息，这里假设 Agent 使用 POST 请求发送 JSON 数据
	if r.Method == http.MethodPost {
		decoder := json.NewDecoder(r.Body)
		err := decoder.Decode(&info)
		if err != nil {
			http.Error(w, "Failed to decode request body", http.StatusBadRequest)
			return
		}

		// 在这里可以对 info 进行处理，存储到数据库、进行分析等
		// ...

		fmt.Println("Received System Info:")
		fmt.Printf("CPU Usage: %.2f%%\n", info.CPUUsage)
		fmt.Printf("Memory Usage: %.2f%%\n", info.MemoryUsage)

		// 返回响应
		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, "Received and processed system info")
	} else {
		http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
	}
}

func main() {
	http.HandleFunc("/system-info", handleSystemInfo)
	fmt.Println("Server listening on :8080")
	http.ListenAndServe(":8080", nil)
}
